/**
 * 根据登录用户的角色权限，筛选前端路由配置数组中的匹配项
 * @param {*} permissions
 * @param {*} routes
 * @returns
 */
const genPermissionRoutes = (permissions, routes) => {
  const result = []
  permissions.forEach(permission => {
    const route = routes.find(route => route.path === permission.menu.url)
    const copy = { ...route }
    // 如果当前权限对象有子级，且当前找到的路由有子级，则递归调用函数继续筛选
    if (permission.children && route.children) {
      const children = genPermissionRoutes(permission.children, route.children)
      copy.children = children
    }

    result.push(copy)
  })

  return result
}

export default genPermissionRoutes
